<?php
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
session_start();

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/account.db.php';
require_once 'db/item.db.php';
require_once 'db/itemType.db.php';
require_once 'db/accountMoney.db.php';
require_once 'db/friend.db.php';
require_once 'db/character.db.php';

//sprintf(param,"user_id=%s&user_pw=%s", g_BravoMain.m_cLoginID, g_BravoMain.m_cLoginPassMD5);//,pMsg->m_byErrStr, pMsg->m_byPhoneNumber);
//echo md5("123456");
$accountId = isset($_GET['GLuser_acc']) ? strval($_GET['GLuser_acc']) : "";
if ($accountId == "") {
    $accountId = isset($_POST['GLuser_acc']) ? strval($_POST['GLuser_acc']) : "";
}
$accountPass = isset($_GET['GLuser_pwd']) ? strval($_GET['GLuser_pwd']) : "";
if ($accountPass == "") {
    $accountPass = isset($_POST['GLuser_pwd']) ? strval($_POST['GLuser_pwd']) : "";
}
/*
  $key = isset($_GET['key']) ? strval($_GET['key']) : "";
  if ($key == "") {
  $key = isset($_POST['key']) ? strval($_POST['key']) : "";
  }

  if ($key != "Glmall0119") {
  return;
  }
 */


/*
  $paperDB = "paper3";
  $tgameDB = "BBGame_Test";
 */

$paperDB = "GL_paper_01";
$tgameDB = "GL_GameDB_01";
/*
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); //将出错信息输出到一个文本文件
 */

$dbc = new DBC();
$balance = 0; //余额

if (isset($_SESSION['AccUnique'])) {
    $accountMoneyDb = new AccountMoneyDb($dbc);
    $accountMoney = $accountMoneyDb->getAccountMoney($_SESSION['AccUnique']);
    if ($accountMoney && isArrayNotEmpty($accountMoney)) {
        $balance = $accountMoney[0]['Balance'];
    }
} else {
    $accountDb = new AccountDb($dbc);
    $user = $accountDb->getAccountByAccountId($accountId);
    
    if (isArrayNotEmpty($user)) {
        $pass = trim($user[0]['AccountPass']);
        $md5 = md5($pass);
        //echo " accountPass=[".$accountPass."]   pass=[".$pass."]  md5=[".$md5."]";
        if ($accountPass != $pass) {
            return;
        }
        $_SESSION['AccUnique'] = $user[0]['AccUnique'];
        $_SESSION['AccountID'] = $user[0]['AccountID'];
        if (isset($_SESSION['AccountID']) && $_SESSION['AccountID'] != "") {
            $accountMoneyDb = new AccountMoneyDb($dbc);
            $accountMoney = $accountMoneyDb->getAccountMoney($_SESSION['AccUnique']);
            if ($accountMoney && isArrayNotEmpty($accountMoney)) {
                $balance = $accountMoney[0]['Balance'];
            }
        }
    }
}

$itemDb = new ItemDb($dbc);

$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$adjacents = isset($_GET['adjacents']) ? intval($_GET['adjacents']) : 0;

if ($page <= 0) {
    $page = 1;
}
if ($adjacents <= 0) {
    $adjacents = 4;
}

$limit = 15;
$start = ($page - 1) * $limit;


if (isset($_GET['type'])) {
    $type = intval($_GET['type']);
} else {
    $type = 1;
}

$data = $itemDb->getItem($type, $start, $limit);

$itemTypeDb = new ItemTypeDb($dbc);
$itemTypeArray = $itemTypeDb->getItemType();

$numberOfObject = $itemDb->getItemNumber($type);

$dbc2 = new DBC($paperDB);
$friendDb = new FriendDb($dbc2);
$friends = $friendDb->getFriendByAccount($_SESSION['AccUnique']);

$dbc3 = new DBC($tgameDB);
$characterDb = new CharacterDb($dbc3);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><?php echo TITLE; ?></title>
        <link rel="stylesheet" href="./css/main.css">
        <!--[if lt IE 9]>
        <script src="js/html5shiv.min.js"></script>
        <script src="js/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="container">
            <div class="product_nav">
                <div class="dianshu">
                    <span class="dianshu_num">荣币：<span id="balance_show"><?php echo $balance; ?></span></span>
                    <a href="http://gcol.qx680.com/" target="_blank" class="chongzhi_btn">在线充值</a>
                </div>
                <a href="store.php?type=1" class="nav_btn"><i class="nav_icon"></i>最新商品</a>
                <a href="store.php?type=2" class="nav_btn"><i class="nav_icon"></i>角色时装</a>
                <a href="store.php?type=3" class="nav_btn"><i class="nav_icon"></i>坐骑装备</a>
                <a href="store.php?type=4" class="nav_btn"><i class="nav_icon"></i>功能道具</a>
                <a href="store.php?type=5" class="nav_btn"><i class="nav_icon"></i>礼物专区</a>
            </div>
            <div class="product_list">
                <?php
                $imageUrl = "http://103.45.101.157:81/RYweb/";
                for ($i = 0; $i < count($data); $i++) {
                    $name = trim($data[$i]['ItemName']);
                    $time_jieshao = trim($data[$i]['time_jieshao']);
                    $alt = trim($data[$i]['alt']);
                    $name = mb_convert_encoding($name, "UTF-8", "GBK");
                    $time_jieshao = iconv("GBK", "UTF-8", $time_jieshao);
                    $alt = iconv("GBK", "UTF-8", $alt);
                    ?>
                    <div class="product_item">
                        <div class="product_img_box">
                            <img id="image_<?php echo $i; ?>" src="<?php echo $imageUrl . $data[$i]['ImageUrl']; ?>" alt="<?php echo $alt; ?>">
                            <input type="hidden" id="item_<?php echo $i; ?>" value="<?php echo $data[$i]['ItemNo']; ?>" />
                            <input type="hidden" id="idx_<?php echo $i; ?>" value="<?php echo $data[$i]['id_x']; ?>" />
                        </div>
                        <div class="product_introl">
                            <div class="detail_introl" id="name_<?php echo $i; ?>"><?php echo $name; ?></div>
                            <div class="product_props">
                                <a href="javascript:void(0)"><?php echo $time_jieshao; ?></a>
                                <a href="javascript:void(0)" id="price_<?php echo $i; ?>"><?php echo $data[$i]['Price']; ?>荣币</a>
                            </div>
                            <div class="product_ctrl">
                                <a href="javascript:void(0)" class="give" onclick="gift_send(<?php echo $i; ?>)">赠送</a>
                                <a href="javascript:void(0)" class="buy" onclick="buy_sdj(<?php echo $i; ?>)">购买</a>
                            </div>
                        </div>
                    </div>
                <?php } ?>

            </div>
        </div>
        <div class="div_overlay" id="modal_div">
            <div id="popupdiv" class="product_item" style="margin: 25% 20%; background-color: wheat; opacity: 1.0;filter:Alpha(opacity=100);">
                <div class="product_img_box" style="margin: 20px;">
                    <img  id="current_image" src="" alt="">
                    <input type="hidden" id="current_item" value="" />
                    <input type="hidden" id="current_idx" value="" />
                </div>
                <div class="product_introl">
                    <div class="detail_introl" id="current_name"><?php echo $name; ?></div>
                    <div class="product_props">
                        <a href="javascript:void(0)">半年</a>
                        <a href="javascript:void(0)" id="current_price">点数</a>
                    </div>
                    <div class="product_ctrl">
                        <a href="javascript:void(0)" class="give" onclick="close_div()">取消</a>
                        <a href="javascript:void(0)" class="buy" onclick="confirm_buy()">确定</a>
                    </div>
                </div>
                <div id="select_div" class="product_introl2">
                    你的好友列表：<br>
                    <select id="friend_id" size="5" style="width:100px;">
                        <?php
                        for ($i = 0, $len = count($friends); $i < $len; $i++) {
                            // $friends[$i]['FriendName'] = mb_convert_encoding(trim($friends[$i]['FriendName']), "UTF-8", "GBK");
                            $name = trim($friends[$i]['FriendName']);
                            $characters = $characterDb->getCharacterByName($name);
                            if (count($characters) > 0) {
                                $name = mb_convert_encoding($name, "UTF-8", "GBK");
                                echo "<option value='" . $characters[0]['AccountUnique'] . "'>$name</option>";
                            }
                        }
                        ?>
                    </select>
                </div>
            </div>
        </div>
    </body>
    <script src="./js/jquery-1.11.3.js"></script>
    <script src="./js/layer.js"></script>
    <script>
                            $(function () {
                                $(document).ready(function () {
                                    $(document).bind("contextmenu", function (e) {
                                        return false;
                                    });
                                });
                                $('.product_list>.product_item:nth-child(3n+1)').css({
                                    //   marginLeft: '0'
                                });
                            });
    </script>
    <script>
        function testgo() {
            layer.msg('您购买的道具已发送至右下角 MY ITEM', 5);
            //layer.msg('你赠送的道具已发送至朋友的右下角 MY ITEM，请提醒他查收',5);
        }
    </script>
    <!--<script type="text/javascript" src="js/jquery.js"></script>-->
    <script type="text/javascript">
        var buy = true;
        function buy_sdj(index) {
            document.getElementById("modal_div").style.display = "block";
            document.getElementById("popupdiv").style.margin = "25% 30%";
            document.getElementById("select_div").style.display = "none";
            document.getElementById("current_image").src = document.getElementById("image_" + index).src;
            document.getElementById("current_name").innerHTML = document.getElementById("name_" + index).innerHTML;
            document.getElementById("current_price").innerHTML = document.getElementById("price_" + index).innerHTML;
            document.getElementById("current_item").value = document.getElementById("item_" + index).value;
            document.getElementById("current_idx").value = document.getElementById("idx_" + index).value;
            buy = true;
        }
        function confirm_buy() {
            var price = document.getElementById("current_price").innerHTML;
            var itemNo = document.getElementById("current_item").value;
            var id_x = document.getElementById("current_idx").value;
            if (buy) {
                var haopengyou = "";
                var index = 0;
            } else {
                var oSelect = document.getElementById('friend_id');
                var index = oSelect.selectedIndex;   //option数组返回的同时会返回当前option的选中下标selectedIndex
                if (index == -1) {
                layer.msg('请选择一个好友', {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
				return;
				}
				var haopengyou = oSelect[index].value;
            }
            
            $.ajax({
                type: "POST",
                url: "api/cart.php",
                dataType: "json",
                data: {
                    action: 'confirmBuy',
                    price: price,
                    itemNo: itemNo,
                    id_x: id_x,
                    buy: buy,
                    haoyou: haopengyou
                },
                success: function (data) {
                    if (data.code == 0) {
                        if (buy) {
//                            alert("购买成功");
                            //layer.msg('购买成功,请从右下角MY ITEM提取', 5);
                            layer.msg('您购买的道具已发送至右下角 MY ITEM', {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
                        } else {
//                            alert("赠送成功");
                            layer.msg('您购买的道具已发送至右下角 MY ITEM', {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
//                            layer.msg('赠送成功', 5);
                        }
                        document.getElementById("modal_div").style.display = "none";
                        document.getElementById("balance_show").innerHTML = data.balance;
                    } else {
//                        alert(data.msg);
                        layer.msg(data.msg, {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
                        document.getElementById("modal_div").style.display = "none";
                    }
                },
                error: function (error, ab, cd) {
                    console.log(error);
                }
            });
        }
        function gift_send(index) {

            document.getElementById("modal_div").style.display = "block";
            document.getElementById("popupdiv").style.margin = "25% 20%";
            document.getElementById("select_div").style.display = "inline-block";
            document.getElementById("current_image").src = document.getElementById("image_" + index).src;
            document.getElementById("current_name").innerHTML = document.getElementById("name_" + index).innerHTML;
            document.getElementById("current_price").innerHTML = document.getElementById("price_" + index).innerHTML;
            document.getElementById("current_item").value = document.getElementById("item_" + index).value;
            document.getElementById("current_idx").value = document.getElementById("idx_" + index).value;
            buy = false;
            /*
             layer.msg("即将开通，敬请期待", 5);*/
        }
        function close_div() {
            document.getElementById("modal_div").style.display = "none";
        }
    </script>
</html>